package ThreadDemo;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-10-19
 * Time: 14:47
 */
public class Demo19 {
    public static void main(String[] args) throws InterruptedException {
        Object o = new Object();
        Thread thread1 = new Thread(() -> {
            System.out.println("thread1 wait()使用之前");
            synchronized(o){
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("thread1执行完成");
        });

        Thread thread3 = new Thread(() -> {
            System.out.println("thread3 wait()使用之前");
            synchronized(o){
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("thread3执行完成");
        });

        Thread thread2 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入值唤醒wait()");
            scanner.nextInt();
            synchronized(o){
                o.notify();
            }
            System.out.println("唤醒成功！");
        });

        Thread thread4 = new Thread(() -> {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入值唤醒wait()");
            scanner.nextInt();
            synchronized(o){
                o.notify();
            }
            System.out.println("唤醒成功！");
        });

        thread1.start();
        thread3.start();
        thread2.start();
        thread4.start();


//        thread1.join();
//        thread2.join();
    }
}
